package com.bw.mvi

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.bw.mvi.mvi.PostsIntent
import com.bw.mvi.mvi.PostsState
import com.bw.mvi.mvi.PostsViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 * mvi------
 * model：数据层
 * view：视图层
 * intent：意图层
 * viewModel：
 * state：状态层
 * (1)view发送intent给viewmodel
 * (2)viewmodel根据不同的意图发起不同请求
 * (3)获得数据更新state
 * (4)view层根据state更新UI
 *
 */
class MainActivity : AppCompatActivity() {
    private val viewModel: PostsViewModel by lazy {
        ViewModelProvider(this)[PostsViewModel::class.java]
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        //1:发送意图
        lifecycleScope.launch {
            viewModel.intents.send(PostsIntent.LoadPosts(1,10))
        }

        //4：订阅状态：根据不同的状态更新UI
        lifecycleScope.launch {
            viewModel.postsFlow_.collect{
                when(it){
                    is PostsState.Empty -> {

                    }
                    is PostsState.Error -> {

                    }
                    is PostsState.Loading -> {

                    }
                    is PostsState.Success -> {

                    }
                }
            }
        }

    }
}